package util
{
	import mx.controls.DateField;
	import mx.formatters.DateFormatter; 
	import spark.formatters.DateTimeFormatter;

	public class DateTimeUtils
	{
		public static const MILLISECOND:Number=1;  
		public static const SECOND:Number=MILLISECOND * 1000;  
		public static const MINUTE:Number=SECOND * 60;  
		public static const HOUR:Number=MINUTE * 60;  
		public static const DAY:Number=HOUR * 24;  
		public static const WEEK:Number=DAY * 7;  
		public static const CHINESE_DATE_FORMAT:String="YYYY-MM-DD";  
		public static const CHINESE_DATETIME_FORMAT:String="YYYY-MM-DD";  
		
		public static function stringToDate(dateString:String, formatString:String=CHINESE_DATE_FORMAT):Date  
		{  
			return DateField.stringToDate(dateString,formatString);  
		}  
		
		
		public static function dateToString(date:Date,formatString:String=CHINESE_DATE_FORMAT):String{  
			return DateField.dateToString(date,formatString);  
		}  
		
		public static function formatDateTime(date:Date, formatString:String=CHINESE_DATE_FORMAT):String  
		{  
			var dateFormater:DateTimeFormatter=new DateTimeFormatter();
			dateFormater.dateTimePattern = formatString;
			return dateFormater.format(date);
		}  
		
		
		public static function formatFullDateTime(date:Date, formatString:String=CHINESE_DATETIME_FORMAT):String  
		{
			var dateFormater:DateFormatter=new DateFormatter();  
			dateFormater.formatString=formatString;  
			return dateFormater.format(date);  
		}
		
		public static function addDaysByDateTime(date:Date, addDay:Number):Date  
		{  
			return new Date(date.getTime() + addDay * DAY);  
		}  
		
		
		public static function  getNextDay(currentDate:Date):Date{  
			return addDaysByDateTime(currentDate,1);  
		}  
		
		
		public static function  getLastDay(currentDate:Date):Date{  
			return addDaysByDateTime(currentDate,-1);  
		}  
		
		public static function addMonths(currentDate:Date,monthInterval:int):Date
		{
			var returnDate:Date=new Date(currentDate.getTime());  
			returnDate.setMonth(returnDate.getMonth() + monthInterval, returnDate.getDate());  
			return returnDate;
		}
		
		public static function getNextMonth(currentDate:Date):Date  
		{  
			var returnDate:Date=new Date(currentDate.getTime());  
			returnDate.setMonth(returnDate.getMonth() + 1, returnDate.getDate());  
			return returnDate;  
		}  
		
		
		public static function getLastMonth(currentDate:Date):Date  
		{  
			var returnDate:Date=new Date(currentDate.getTime());  
			returnDate.setMonth(returnDate.getMonth() - 1, returnDate.getDate());  
			return returnDate;  
		}  
		
		
		public static function getNextYear(currentDate:Date):Date  
		{  
			var returnDate:Date=new Date(currentDate.getTime());  
			returnDate.setFullYear(returnDate.getFullYear() + 1);  
			return returnDate;  
		}  
		
		
		public static function getLastYear(currentDate:Date):Date  
		{  
			var returnDate:Date=new Date(currentDate.getTime());  
			returnDate.setFullYear(returnDate.getFullYear() - 1);  
			return returnDate;  
		}  
		
		
		
		public static function getFristDayOfMonth(currentDate:Date):Date  
		{  
			currentDate.setMonth(currentDate.getMonth(), 1); //下个月的第一天，也就是下个月1号  
			return currentDate;  
		}  
		
		
		public static function getLastDayOfMonth(currentDate:Date):Date  
		{  
			currentDate.setMonth(currentDate.getMonth() + 1, 1); //下个月的第一天，也就是下个月1号  
			currentDate.setDate(currentDate.getDate() - 1); //下个月1号之前1天，也就是本月月底  
			return currentDate;  
		}  
		
		
		
		public static function getChineseDay(currentDate:Date):String  
		{  
			switch (currentDate.getDay())  
			{  
				case 0:  
					return "星期日";  
					
				case 1:  
					return "星期一";  
					
				case 2:  
					return "星期二";  
					
				case 3:  
					return "星期三";  
					
				case 4:  
					return "星期四";  
					
				case 5:  
					return "星期五";  
					
				case 6:  
					return "星期六";  
					
				default:  
					return "";  
			}  
		}  
		
		
		public static function getEnglishDay(currentDate:Date):String  
		{  
			switch (currentDate.getDay())  
			{  
				case 0:  
					return "Sunday";  
					
				case 1:  
					return "Monday";  
					
				case 2:  
					return "Tuesday";  
					
				case 3:  
					return "Wednesday";  
					
				case 4:  
					return "Thursday";  
					
				case 5:  
					return "Friday";  
					
				case 6:  
					return "Saturday";  
					
				default:  
					return "";  
			}  
		}
	}
}